package com.rxt.thermopro.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import com.rxt.thermopro.R
import com.rxt.thermopro.app.LocalDataDao
import com.rxt.thermopro.bean.ProbeHistoryWrapper
import com.rxt.thermopro.ex.is24HourStyle
import com.rxt.thermopro.ui.fragment.HistoryFragment
import kotlinx.android.synthetic.main.item_view_history_list.view.*

class HistoryListAdapter(var list: MutableList<ProbeHistoryWrapper>, val isTP960: Boolean) :
    RecyclerView.Adapter<HistoryListAdapter.MyViewHolder>() {

    inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

    }

    var onClick: OnItemClickListener? = null

    interface OnItemClickListener {
        fun onItemClick(view: View, position: Int)
    }

    fun setOnItemClick(onItemClickListener: OnItemClickListener) {
        this.onClick = onItemClickListener
    }

    var presenter: HistoryFragment.HistoryPresenter? = null

    fun setDevicePresenter(presenter: HistoryFragment.HistoryPresenter) {
        this.presenter = presenter
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val itemView =
            LayoutInflater.from(parent.context)
                .inflate(R.layout.item_view_history_list, parent, false)
        return MyViewHolder(itemView)
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.itemView.viewIndex.text = (position + 1).toString() + "."
//        holder.itemView.viewTime.text =
//            LocalDataDao.INSTANCE.dateFormat.format(list[position].lastTime.toLong())
        holder.itemView.viewTime.text =
            LocalDataDao.INSTANCE.formatDateTime(list[position].lastTime.toLong()) + "  " +
                    LocalDataDao.INSTANCE.formatTimeAMPM(
                        list[position].lastTime.toLong(),
                        is24HourStyle(holder.itemView.context)
                    )
        if (isTP960) {
            holder.itemView.viewProbe.text = ""
            holder.itemView.viewProbe.isVisible=false
        } else {
            holder.itemView.viewProbe.isVisible=true
            holder.itemView.viewProbe.text = "Probe ${list[position].probeIndex + 1}"
        }
        holder.itemView.viewProfile.text = list[position].profile.name

        holder.itemView.setOnClickListener {
            presenter?.onItemClick(holder.itemView, position)
        }

        holder.itemView.viewDeleteBBQ.setOnClickListener {
            presenter?.deleteItem(list[position], position)
        }

    }

    override fun getItemCount(): Int {
        return list.size
    }

    fun addAll(itList: List<ProbeHistoryWrapper>) {
        list.addAll(itList)
        notifyDataSetChanged()
    }

    fun remove(position: Int) {
        list.removeAt(position)
        notifyItemRangeRemoved(position, 1)
        notifyItemRangeChanged(position, itemCount)
    }


}